package cn.tedu.huntingmall.service.impl;

import cn.tedu.huntingmall.commons.exception.ServiceException;
import cn.tedu.huntingmall.commons.response.ServiceCode;
import cn.tedu.huntingmall.mapper.MyOrderMapper;
import cn.tedu.huntingmall.mapper.StockMapper;
import cn.tedu.huntingmall.mapper.UserMapper;
import cn.tedu.huntingmall.pojo.dto.myorder.MyOrderAddNewDTO;
import cn.tedu.huntingmall.pojo.entity.myorder.MyOrder;
import cn.tedu.huntingmall.pojo.entity.stock.Stock;
import cn.tedu.huntingmall.pojo.entity.user.User;
import cn.tedu.huntingmall.pojo.vo.myorder.MyOrderStandardVO;
import cn.tedu.huntingmall.pojo.vo.user.UserStandardVO;
import cn.tedu.huntingmall.service.IMyOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static cn.tedu.huntingmall.commons.response.ServiceCode.*;

@Service
@Slf4j
public class MyOrderServiceImpl implements IMyOrderService {

    @Autowired
    private MyOrderMapper myOrderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StockMapper stockMapper;

    @Override
    public Long insert(MyOrderAddNewDTO myOrderAddNewDTO) {
        String username = myOrderAddNewDTO.getUsername();
        UserStandardVO user = userMapper.selectByUsername(username);
        if (user == null) {
            log.debug("未找到对应用户名:{}的商品,请稍后重试!", username);
            throw new ServiceException(NOT_FOUND, "请先注册后再尝试购买!");
        }
        Long commodityId = myOrderAddNewDTO.getCommodityId();
        Integer count = myOrderAddNewDTO.getCount();
        Stock stock = stockMapper.selectByCommId(commodityId);
        Integer countInDB = stock.getCount();
        if (countInDB < count){
            log.debug("当前库存为{},库存不足,请重新选择购买数量!", count);
            throw new ServiceException(CONFLICT, "库存不足,请重新选择购买数量!");
        }
        MyOrder myOrder = new MyOrder();
        BeanUtils.copyProperties(myOrderAddNewDTO, myOrder);
        double money = myOrderAddNewDTO.getPrice() * count;
        myOrder.setMoney(money);
        Long id = userMapper.selectByUsername(username).getId();
        myOrder.setUserId(id);
        LocalDateTime now = LocalDateTime.now();
        myOrder.setGmtCreate(now);
        myOrder.setGmtModified(now);

        int rows = myOrderMapper.insert(myOrder);
        if (rows != 1){
            log.error("服务器忙,请稍后重试!");
            throw new ServiceException(INTERNAL_SERVER_ERROR, "服务器忙,请稍后重试!");
        }

        int updatecount = stockMapper.updatecount(count, commodityId);
        if (updatecount != 1){
            log.error("服务器忙,请稍后重试!");
            throw new ServiceException(INTERNAL_SERVER_ERROR, "服务器忙,请稍后重试!");
        }

        return myOrder.getId();
    }

    @Override
    public int deleteById(Long id, String username) {
        UserStandardVO user = userMapper.selectByUsername(username);
        if (user == null) {
            log.debug("未找到对应用户名:{}的商品,请稍后重试!", username);
            throw new ServiceException(NOT_FOUND, "请先注册后再尝试购买!");
        }
        MyOrderStandardVO myOrder = myOrderMapper.selectById(id);
        if (myOrder == null) {
            log.error("未找到相应id:{} 商品信息,请稍后重试!", id);
            throw new ServiceException(NOT_FOUND, "未找到相应商品信息,请稍后重试!");
        }
        int rows = myOrderMapper.deleteById(id);
        if (rows != 1){
            log.error("服务器忙,请稍后重试!");
            throw new ServiceException(INTERNAL_SERVER_ERROR, "服务器忙,请稍后重试!");
        }
        return 1;
    }

    @Override
    public List<MyOrder> selectByUsername(String username) {
        UserStandardVO user = userMapper.selectByUsername(username);
        if (user == null) {
            log.debug("未找到对应用户名:{}的商品,请稍后重试!", username);
            throw new ServiceException(NOT_FOUND, "请先登录后再尝试购买!");
        }
        return myOrderMapper.selectByUsername(username);
    }
}
